<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row type="flex" align="middle">
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="信息">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
                <el-input v-model="query.keywords" placeholder="请输入优惠券信息"></el-input>
              </el-form-item>
              <el-form-item style="margin-bottom:0px" label="类型">
                <el-select v-model="query.type" placeholder="请选择优惠券类型">
                  <el-option label="全部" value="0"></el-option>
                  <el-option label="单独发放" value="1"></el-option>
                  <el-option label="批量发放" value="2"></el-option>
                  <el-option label="删除用户优惠券" value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格 -->
    <el-card class="box-card">
      <img
        class="watermark"
        v-if="baseApi== 'https://testouhun.wm2177.com'"
        style="left: 0px;"
        src="@/assets/watermark.png"
      />
      <el-table
        :data="tableData"
        border
        style="width: 100%;margin-bottom: 20px;"
        v-loading="loading"
        :header-cell-style="{background:'#eee'}"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="coupon_info.title" label="优惠券名称"></el-table-column>
        <el-table-column align="center" prop="admin_name" label="操作人"></el-table-column>
        <el-table-column align="center" prop="nickname" label="用户"></el-table-column>
        <el-table-column align="center" prop="type" label="类型">
          <template slot-scope="scope">
            <div v-if="scope.row.type==1">单独发放</div>
            <div v-if="scope.row.type==2">批量发放</div>
            <div v-if="scope.row.type==3">删除用户优惠券</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="file_before" label="源文件">
          <template slot-scope="scope">
            <div v-if="scope.row.type!=2">-</div>
            <el-button v-else type="text" @click="loadY(scope.row.file_before)">下载源文件</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="file_result" label="结果文件">
          <template slot-scope="scope">
            <div v-if="scope.row.type!=2">-</div>
            <el-button v-else type="text" @click="loadR(scope.row.file_result)">下载结果文件</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="num" label="发放数量">
          <template slot-scope="scope">
            <div v-if="scope.row.num==0">-</div>
            <div v-else>{{scope.row.num}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="发放时间"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { sendList } from '@/api/coupon'
import { parseTime } from '@/utils/index'

export default {
  name: 'CouponSendList',
  data() {
    return {
      baseApi: process.env.VUE_APP_BASE_API,
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //input
      query: {
        keywords: '',
        type: '0'
      },
      tableData: [],
      loading: false
    }
  },
  mounted() {
    this.getsendList()
  },
  activated() {
    this.currentPage = 1
    this.query.keywords = ''
    this.query.type = '0'
    this.getsendList()
  },
  methods: {
    // 获取登录日志
    async getsendList() {
      this.loading = true
      const res = await sendList({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.keywords,
        type: this.query.type
      })
      this.loading = false
      console.log(res)
      this.tableData = res.data
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
      })
      this.total = res.count
    },
    queryAsk() {
      this.currentPage = 1
      this.getsendList()
    },
    //重置
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.keywords = ''
      this.query.type = '0'
      this.getsendList()
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getsendList()
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.drawer = false
    },
    loadY(url) {
      window.open(url)
    },
    loadR(url) {
      window.open(url)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
  .el-form {
    display: flex;
  }
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>